<template>
  <div class="box" :id="EchartId"></div>
</template>

<script>
import uuidv1 from 'uuid/v1';
import { buildOption, buildTitle, buildGrid, buildXAxis, buildYAxis, buildPieSeries, buildLineSeries, buildSeries } from './buildEChartOption';
import { buildDateAxis } from '@/unit/tool';

function updataCharts(newVal, _this) {
  let { dataLists, ...restOption } = newVal;
  let pieLists = [];
  if (dataLists && dataLists.length > 0) {
    let pieSeries = buildPieSeries(dataLists);
    let seriesLists = buildSeries(pieSeries);
    _this.myChart.setOption({
      ...restOption,
      series: seriesLists,
    });
  }
}

export default {
  name: 'normalPie',
  props: {
    pieData: undefined,
  },
  data() {
    return {
      myChart: '',
      EchartId: '',
    };
  },
  watch: {
    pieData: function(newVal) {
      // debugger;
      // let { dataLists, ...restOption } = newVal;
      // let pieLists = [];
      // if (dataLists.length > 0) {
      //   let pieSeries = buildPieSeries(dataLists);
      //   let seriesLists = buildSeries(pieSeries);
      //   this.myChart.setOption({
      //     ...restOption,
      //     series: seriesLists,
      //   });
      // }
      updataCharts(newVal, this);
    },
  },
  methods: {
    init_chart() {
      // 绘制图表
      let myChart = this.myChart;

      let option = buildOption({
        grid: buildGrid(),
      });

      myChart.setOption(option);

      if (this.pieData != undefined && this.pieData != null) {
        updataCharts(this.pieData, this);
      }
    },
  },
 mounted() {
    this.EchartId = uuidv1(); //获取随机id
  },
  mounted() {
    // debugger;

    // 基于准备好的dom，初始化echarts实例
    this.myChart = this.$echarts.init(document.getElementById(this.EchartId));
    this.init_chart();

    // let { dataLists, ...restOption } = this.pieData;
    // let pieLists = [];
    // if (dataLists.length > 0) {
    //   let pieSeries = buildPieSeries(dataLists);
    //   let seriesLists = buildSeries(pieSeries);
    //   this.myChart.setOption({
    //     ...restOption,
    //     series: seriesLists,
    //   });
    // }
  },
  destroyed() {
    this.myChart.clear();
  },
};
</script>

<style scoped="scoped">
.box {
  width: 100%;
  height: 100%;
  background: transparent;
}
</style>
